package com.webwork.lims.stock.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.stock.dao.ProductDao;
import com.webwork.lims.stock.dataobject.Product;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;
import com.webwork.lims.util.StrManager;

@Repository
public class ProductDaoImpl extends SimpleDao implements ProductDao {

	public ProductDaoImpl() {
		super();
		this.dbTable = "t_product";
		dbColumns.clear();
		dbColumns.put("sku", "SKU");
		dbColumns.put("productName", "PRODUCT_NAME");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Product))
			return new ColumnLimit();
		Product o = (Product) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getSku())) {
			ret.add(dbColumns.get("sku") + " like ");
		}
		if (!StrManager.isEmpty(o.getProductName())) {
			ret.add(dbColumns.get("productName") + " like ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Product o = (Product) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getSku())) {
			ret.add("%" + o.getSku() + "%");
		}
		if (!StrManager.isEmpty(o.getProductName())) {
			ret.add("%" + o.getProductName() + "%");
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Product o = (Product) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getSku());
		objs.add(o.getProductName());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Product ret = new Product();
		ret.setSku((String) map.get(dbColumns.get("sku")));
		ret.setProductName((String) map.get(dbColumns.get("productName")));
		return ret;
	}
	
	@Override
	public void insert(Product o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Product o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("sku") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getSku());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String sku) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("sku") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, sku);
	}

	@Override
	public List<Product> query(Product o, int page, int size) {
		List<Product> ret = new ArrayList<Product>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Product) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Product o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
